package com.example.calculator.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.example.calculator.MyViewModel

import com.example.calculator.R
import com.example.calculator.databinding.FragmentQuestionBinding

import kotlinx.android.synthetic.main.fragment_question.*
import kotlinx.coroutines.runBlocking
import java.lang.StringBuilder


/**
 * A simple [Fragment] subclass.
 * Use the [QuestionFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class QuestionFragment : Fragment() {

    private var mViewModel:MyViewModel?=null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
         mViewModel= ViewModelProvider(activity!!)[MyViewModel::class.java]

        //
        val databinding: FragmentQuestionBinding =
            DataBindingUtil.inflate(inflater,R.layout.fragment_question,container,false)
        databinding.data=mViewModel
        databinding.lifecycleOwner=activity
        // Inflate the layout for this fragment
        return databinding.root
        // Inflate the layout for this fragment

    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mViewModel!!.generator()
        mViewModel!!.getCurrentScore().value=0
        btn_1.setOnClickListener(listener)
        btn_2.setOnClickListener(listener)
        button3.setOnClickListener(listener)
        button4.setOnClickListener(listener)
        button5.setOnClickListener(listener)
        button6.setOnClickListener(listener)
        button7.setOnClickListener(listener)
        button8.setOnClickListener(listener)
        button9.setOnClickListener(listener)
        button11.setOnClickListener(listener)
        button10.setOnClickListener(View.OnClickListener {
            builder.setLength(0)
            refreshMsg()
        })
        //提交结果
        button12.setOnClickListener(View.OnClickListener {
            var num1:Int=mViewModel!!.getExpression().value!!.num1
            var num2:Int=mViewModel!!.getExpression().value!!.num2
            var operator:Char=mViewModel!!.getExpression().value!!.operator
            var corret:Int=0
            when(operator){
                '+'->{
                    corret=num1+num2
                }
                '-'->{
                    corret=num1-num2
                }
            }
            //回答正确
          if (builder.toString().isNotEmpty() &&builder.toString().toInt()==corret){
              mViewModel!!.answerCorret()
              builder.setLength(0)
              textView9.text=(getString(R.string.answer_correct_msg))
//              refreshMsg()
          }else{
              //回答错误但是超出最高分
              if (mViewModel!!.winFlag){
                  mViewModel!!.winFlag=false
                  Navigation.findNavController(it)
                      .navigate(R.id.action_questionFragment_to_winFragment)
              }else{ //回答错误没有超过最高分
                  Navigation.findNavController(it)
                      .navigate(R.id.action_questionFragment_to_failedFragment)
              }
          }
        })

    }
    var builder=StringBuilder()
    var listener=View.OnClickListener{
        when(it.id){
            R.id.button11->{
                builder.append(0)
            }
            R.id.btn_1->{
                builder.append(1)
            }
            R.id.btn_2->{
                builder.append(2)
            }
            R.id.button3->{
                builder.append(3)
            }
            R.id.button4->{
                builder.append(4)
            }
            R.id.button5->{
                builder.append(5)
            }
            R.id.button6->{
                builder.append(6)
            }
            R.id.button7->{
                builder.append(7)
            }
            R.id.button8->{
                builder.append(8)
            }
            R.id.button9->{
                builder.append(9)
            }
            R.id.button10->{
                builder.setLength(0)
            }

        }
        if (builder.isEmpty()){
            textView9.text = getString(R.string.your_answer)
        }else{
            refreshMsg()
        }
    }

    private fun refreshMsg() {
        textView9.text = builder.toString()
    }

    override fun onResume() {
        super.onResume()
        mViewModel!!.generator()
        builder.setLength(0)
    }


}
